package pack;

import java.io.Serializable;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import dao.BiletDAORemote;
import dto.BiletDTO;
import dto.EmployeeDTO;

@ManagedBean(name = "biletBean2")
@RequestScoped
public class BiletBean2 implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	

	@EJB
	BiletDAORemote biletDAO;

	@ManagedProperty(value = "#{biletDTO}")
	private BiletDTO biletDTO; // = new EmployeeDTO();

	ExternalContext exCon;
	private int id;
	private int empl_id;
	private String status;
	private String motiv;
	private int tl_id;
	private String firstname;
	private String lastname;
	private String start_an;
	private String start_luna;
	private String start_zi;
	private String start_ora;
	private String start_min;
	private String end_an;
	private String end_luna;
	private String end_zi;
	private String end_ora;
	private String end_min;

	private int biletId;

	private String butonId;

	FacesContext fc = FacesContext.getCurrentInstance();

	List<BiletDTO> bilete;
	
	public void verify() {
		EmployeeDTO eDTO = (EmployeeDTO) fc.getExternalContext()
				.getSessionMap().get("loggedInUser");

		ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc
				.getApplication().getNavigationHandler();

		if (eDTO != null) {
			if (eDTO.getRank() != 5) {
				nav.performNavigation("home.xhtml");
			}
		} else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}

	public String read() {
		EmployeeDTO eDTO = new EmployeeDTO();
		eDTO = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		bilete = biletDAO.findAllTickets2(eDTO);
		this.setBilete(bilete);
		fc.getExternalContext().getSessionMap().put("loggedIn", eDTO);
		return "";
	}
	
	public String read2() {
		EmployeeDTO eDTO = new EmployeeDTO();
		eDTO = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		bilete = biletDAO.findAllTickets(eDTO);
		this.setBilete(bilete);
		return "";
	}

	public String getAllBilete() {
		System.out.println("Bejon");
		EmployeeDTO eDTO = new EmployeeDTO();
		eDTO = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		setBilete(biletDAO.findAllTickets(eDTO));
		this.setBilete(bilete);
		return "";

	}
	
	public String send() {
		EmployeeDTO ee = new EmployeeDTO();
		ee = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("loggedInUser");
		biletDTO = new BiletDTO();
		biletDTO.setStatus("In asteptare");
		biletDTO.setEmpl_id(ee.getId());
		biletDTO.setMotiv(getMotiv());
		biletDTO.setStart_time(getStart_time());
		biletDTO.setEnd_time(getEnd_time());
		EmployeeDTO e = (EmployeeDTO) fc.getExternalContext().getSessionMap()
				.get("tl_id");
		biletDTO.setTl_id(e.getId());
		biletDTO.setFirstname(ee.getFirstname());
		biletDTO.setLastname(ee.getLastname());
		try {
			biletDAO.addBilet(biletDTO);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		resetFields();
		return "";

	}
	
	public void resetFields() {
		setMotiv("");
		setStart_an("2013");
		setStart_luna("01");
		setStart_zi("01");
		setStart_ora("08");
		setStart_min("00");
		setEnd_an("2013");
		setEnd_luna("01");
		setEnd_zi("01");
		setEnd_ora("08");
		setEnd_min("00");
	}
	
	public String getStart_time() {
		return this.setDateStart();
	}

	
	public String setDateStart() {
		String dd = "";
		dd = dd + getStart_an() + "-" + getStart_luna() + "-" + getStart_zi()
				+ " " + getStart_ora() + ":" + getStart_min() + ":00.0";
		return dd;
	}
	
	public String getEnd_time() {
		return this.setDateEnd();
	}

	public String setDateEnd() {
		String dd = "";
		dd = dd + getEnd_an() + "-" + getEnd_luna() + "-" + getEnd_zi()
				+ " " + getEnd_ora() + ":" + getEnd_min() + ":00.0";
		return dd;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getEmpl_id() {
		return empl_id;
	}

	public void setEmpl_id(int empl_id) {
		this.empl_id = empl_id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMotiv() {
		return motiv;
	}

	public void setMotiv(String motiv) {
		this.motiv = motiv;
	}

	public int getTl_id() {
		return tl_id;
	}

	public void setTl_id(int tl_id) {
		this.tl_id = tl_id;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getStart_an() {
		return start_an;
	}

	public void setStart_an(String start_an) {
		this.start_an = start_an;
	}

	public String getStart_luna() {
		return start_luna;
	}

	public void setStart_luna(String start_luna) {
		this.start_luna = start_luna;
	}

	public String getStart_zi() {
		return start_zi;
	}

	public void setStart_zi(String start_zi) {
		this.start_zi = start_zi;
	}

	public String getStart_ora() {
		return start_ora;
	}

	public void setStart_ora(String start_ora) {
		this.start_ora = start_ora;
	}

	public String getStart_min() {
		return start_min;
	}

	public void setStart_min(String start_min) {
		this.start_min = start_min;
	}

	public String getEnd_an() {
		return end_an;
	}

	public void setEnd_an(String end_an) {
		this.end_an = end_an;
	}

	public String getEnd_luna() {
		return end_luna;
	}

	public void setEnd_luna(String end_luna) {
		this.end_luna = end_luna;
	}

	public String getEnd_zi() {
		return end_zi;
	}

	public void setEnd_zi(String end_zi) {
		this.end_zi = end_zi;
	}

	public String getEnd_ora() {
		return end_ora;
	}

	public void setEnd_ora(String end_ora) {
		this.end_ora = end_ora;
	}

	public String getEnd_min() {
		return end_min;
	}

	public void setEnd_min(String end_min) {
		this.end_min = end_min;
	}

	public int getBiletId() {
		return biletId;
	}

	public void setBiletId(int biletId) {
		this.biletId = biletId;
	}

	public String getButonId() {
		return butonId;
	}

	public void setButonId(String butonId) {
		this.butonId = butonId;
	}

	public List<BiletDTO> getBilete() {
		return bilete;
	}

	public void setBilete(List<BiletDTO> bilete) {
		this.bilete = bilete;
	} 
	
	public BiletDTO getBiletDTO() {
		return biletDTO;
	}

	public void setBiletDTO(BiletDTO biletDTO) {
		this.biletDTO = biletDTO;
	}

	

}
